package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.RoleRes;
import org.stvd.repository.admin.RoleResDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("RoleResDao")
public class RoleResDaoImpl extends BaseDaoImpl<RoleRes> implements RoleResDao {

    @Override
    public List<RoleRes> findRoleResByUid(String roleId, String guid) {
        return super.findByHQL("from RoleRes T where T.roleId= ?0 and T.guid = ?1", roleId, guid);
    }

    @Override
    public void insertRoleRes(String roleId, String[] resIds, String guid) {
        em.createQuery("delete from RoleRes T where T.roleId = :roleId and T.guid = :guid")
        .setParameter("roleId", roleId)
        .setParameter("guid", guid)
        .executeUpdate();
        em.flush();
        if (resIds != null && resIds.length > 0) {
            for (String resId : resIds) {
                RoleRes roleresTmp = new RoleRes();
                roleresTmp.setId(StringUtil.getRandomUUID());
                roleresTmp.setRoleId(roleId);
                roleresTmp.setResId(resId);
                roleresTmp.setGuid(guid);
                insert(roleresTmp);
            }
        }
    }
}
